/**
 * Description: StudentAction.java
 * Copyright (c) 2008-2009  Neo. 
 * All Rights Reserved.
 * @version 1.0  Mar 24, 2009 2:51:48 PM 李萌（neolimeng@gmail.com）created
 */
package org.paradise.dms.web.action.student;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.directwebremoting.annotations.RemoteProxy;
import org.paradise.dms.poi.NeoPoiUtil;
import org.paradise.dms.poi.StudentPoi;
import org.paradise.dms.pojo.Student;
import org.paradise.dms.services.StudentService;
import org.paradise.dms.util.DateJsonValueProcessor;
import org.paradise.dms.util.JsonUtil;
import org.paradise.dms.util.UploadUtil;
import org.paradise.dms.web.action.DMSBaseAction;
import org.paradise.dms.web.tools.DWRPage;
import org.paradise.dms.web.tools.Pager;
import org.paradise.dms.web.tools.PagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * Description: Copyright (c) 2008-2009 Neo. All Rights Reserved.
 * 
 * @version 1.0 Mar 24, 2009 2:51:48 PM 李萌（neolimeng@gmail.com）created
 */
@Service
@RemoteProxy(name = "studentAction")
public class StudentAction extends DMSBaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8717985433812076050L;
	private static Logger log = Logger.getLogger(StudentAction.class);
	@Autowired
	@Qualifier("studentServiceImpl")
	private StudentService studentservice;
	private Student student;
	private List<Student> sl;// StudentsList
	private int studentid;
	private String searchType;
	private String studentForSearch;

	@Autowired
	private PagerService pagerService;
	private Pager pager;
	protected String currentPage;
	protected String totalRows;
	protected String pagerMethod;

	// 与上传照片有关的信息
	// 封装上传文件域的属性
	private File uploadedFile;
	// 封装上传文件类型的属性
	private String uploadedFileContentType;
	// 封装上传文件名的属性
	private String uploadedFileFileName;
	// 照片保存路径
	private String studentPicSavePath;

	private String studentIdListForExcel;

	private String studentIdListForExcel2;

	public String getStudentIdListForExcel2() {
		return studentIdListForExcel2;
	}

	public void setStudentIdListForExcel2(String studentIdListForExcel2) {
		this.studentIdListForExcel2 = studentIdListForExcel2;
	}

	public String getStudentIdListForExcel() {
		return studentIdListForExcel;
	}

	public void setStudentIdListForExcel(String studentIdListForExcel) {
		this.studentIdListForExcel = studentIdListForExcel;
	}

	/**
	 * @return the student
	 */
	public Student getStudent() {
		return student;
	}

	/**
	 * @param student
	 *            the student to set
	 */
	public void setStudent(Student student) {
		this.student = student;
	}

	/**
	 * @return the studentservice
	 */
	public StudentService getStudentservice() {
		return studentservice;
	}

	/**
	 * @param studentservice
	 *            the studentservice to set
	 */
	public void setStudentservice(StudentService studentservice) {
		this.studentservice = studentservice;
	}

	/**
	 * @return the sl
	 */
	public List<Student> getSl() {
		return sl;
	}

	/**
	 * @param sl
	 *            the sl to set
	 */
	public void setSl(List<Student> sl) {
		this.sl = sl;
	}

	public PagerService getPagerService() {
		return pagerService;
	}

	public void setPagerService(PagerService pagerService) {
		this.pagerService = pagerService;
	}

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public String getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(String currentPage) {
		this.currentPage = currentPage;
	}

	public String getTotalRows() {
		return totalRows;
	}

	public void setTotalRows(String totalRows) {
		this.totalRows = totalRows;
	}

	public String getPagerMethod() {
		return pagerMethod;
	}

	public void setPagerMethod(String pagerMethod) {
		this.pagerMethod = pagerMethod;
	}

	public File getUploadedFile() {
		return uploadedFile;
	}

	public void setUploadedFile(File uploadedFile) {
		this.uploadedFile = uploadedFile;
	}

	public String getUploadedFileContentType() {
		return uploadedFileContentType;
	}

	public void setUploadedFileContentType(String uploadedFileContentType) {
		this.uploadedFileContentType = uploadedFileContentType;
	}

	public String getUploadedFileFileName() {
		return uploadedFileFileName;
	}

	public void setUploadedFileFileName(String uploadedFileFileName) {
		this.uploadedFileFileName = uploadedFileFileName;
	}

	@SuppressWarnings("deprecation")
	public String getStudentPicSavePath() {
		return ServletActionContext.getRequest()
				.getRealPath(studentPicSavePath);
	}

	public void setStudentPicSavePath(String studentPicSavePath) {
		this.studentPicSavePath = studentPicSavePath;
	}

	public int getStudentid() {
		return studentid;
	}

	public void setStudentid(int studentid) {
		this.studentid = studentid;
	}

	public String getSearchType() {
		return searchType;
	}

	public void setSearchType(String searchType) {
		this.searchType = searchType;
	}

	public String getStudentForSearch() {
		return studentForSearch;
	}

	public void setStudentForSearch(String studentForSearch) {
		this.studentForSearch = studentForSearch;
	}

	/**
	 * 
	 * Description: 插入一条学生记录
	 * 
	 * @Version1.0 Mar 24, 2009 3:04:30 PM 李萌（neolimeng@gmail.com）创建
	 * @return
	 */
	public String insertStudent() {
		// 照片保存的文件名：学号+照片类型后缀
		String studentPicFileName = this.student.getStudentno() + ".jpg";
		// 指定照片保存路径
		String saveStudentPicSavePath = this.getStudentPicSavePath() + "\\"
				+ studentPicFileName;

		if (null != this.getUploadedFile()) {// 上传文件不为空
			if (UploadUtil.uploadFile(this.getUploadedFile(),
					saveStudentPicSavePath)) {
				// 上传照片成功
				this.student.setStudentpicturepath(this.studentPicSavePath
						+ studentPicFileName);
				// System.out.println("学生照片保存位置：" + this.studentPicSavePath +
				// studentPicFileName);
			}
		}
		student.setStudentgraduatedate(new Date());
		System.out.println("**************");
		if (this.studentservice.insertStudent(student)) {
			return SUCCESS;
		} else
			return INPUT;
	}

	/**
	 * 
	 * Description: 更新一条学生记录
	 * 
	 * @Version1.0 Mar 24, 2009 3:13:44 PM 李萌（neolimeng@gmail.com）创建
	 * @return
	 */
	public String updateStudent() {
		Student s = this.studentservice.getStudent(student.getStudentid());
		String tempEnrollYear = s.getStudentenrollyear();

		// s.setStudentpassword(student.getStudentpassword());
		s.setStudentname(student.getStudentname());
		s.setStudentgender(student.getStudentgender());
		s.setStudentbirthday(student.getStudentbirthday());
		s.setStudentidentitycardno(student.getStudentidentitycardno());
		s.setStudentbirthplace(student.getStudentbirthplace());
		s.setStudentethnic(student.getStudentethnic());
		s.setStudenttype(student.getStudenttype());
		s.setStudentenrolltype(student.getStudentenrolltype());
		s.setStudenteducationyears(student.getStudenteducationyears());
		s.setStudentadmissiontype(student.getStudentadmissiontype());
		s.setStudentenrollyear(student.getStudentenrollyear());
		s.setStudenttelephone(student.getStudenttelephone());
		s.setStudenthomeaddress(student.getStudenthomeaddress());
		s.setStudentarchivesaddress(student.getStudentarchivesaddress());
		s.setStudentmisc(student.getStudentmisc());
		if (this.studentservice.updateStudent(s)) {
			// log.info("@@@" + student.getStudentname());
			// log.info("##"+this.getSearchType());
			// if("stuno".equals(searchType)){
			// this.setStudentForSearch(s.getStudentno());
			// }else this.setStudentForSearch(s.getStudentname());
			// this.getRequest().setAttribute("searchType", searchType);
			// this.getRequest().setAttribute("studentForSearch",
			// studentForSearch);

			// 如果更改了入学年份
			if (!tempEnrollYear.equals(student.getStudentenrollyear())) {
				studentservice.updateStudentChargeInfo(student.getStudentid()
						+ "", tempEnrollYear, student.getStudentenrollyear());
			}
			this.getRequest().setAttribute("studentno", student.getStudentno());
			return SUCCESS;
		} else
			return INPUT;
	}

	/**
	 * 
	 * Description: 删除一条学生记录
	 * 
	 * @Version1.0 Mar 24, 2009 3:14:00 PM 李萌（neolimeng@gmail.com）创建 changed by
	 *             zhengxu for dwr
	 * @return
	 */
	public String deleteStudent(int studentid) {
		try {
			if (this.studentservice.deleteStudent(studentid))
				return SUCCESS;
			else
				return INPUT;
		} catch (Exception e) {
			return INPUT;

		}
	}

	/**
	 * 
	 * Description: 删除一条学生记录
	 * 
	 * @Version1.0 Mar 24, 2009 3:14:00 PM 李萌（neolimeng@gmail.com）创建 changed by
	 * 
	 * @return
	 */
	public String deleteStudent() {
		try {
			if (this.studentservice.deleteStudent(studentid))
				return SUCCESS;
			else
				return INPUT;
		} catch (Exception e) {
			return INPUT;

		}
	}

	/**
	 * 
	 * Description: 根据学生ID取得一条学生记录
	 * 
	 * @Version1.0 Mar 24, 2009 3:14:13 PM 李萌（neolimeng@gmail.com）创建
	 * @return
	 */
	public String getStudentByStudentId() {
		try {
			int stid = Integer.parseInt(ServletActionContext.getRequest()
					.getParameter("studentid"));
			student = this.studentservice.getStudent(stid);
			return SUCCESS;
		} catch (Exception e) {
			log.error("DMS_error:StudentAction中根据学生ID取得一条学生记录时出错");
			log.error("错误原因:" + e);
			return INPUT;
		}
	}

	/**
	 * 
	 * Description: 取得所有学生记录
	 * 
	 * @Version1.0 Mar 24, 2009 3:14:32 PM 李萌（neolimeng@gmail.com）创建
	 * @return
	 */
	public String getAllStudentsPerPage() {
		int totalRow = studentservice.getAllStudentRows();
		pager = pagerService.getPager(this.getCurrentPage(), this
				.getPagerMethod(), totalRow);
		this.setCurrentPage(String.valueOf(pager.getCurrentPage()));
		this.setTotalRows(String.valueOf(totalRow));
		sl = this.studentservice.getAllStudentPerPage(pager.getPageSize(),
				pager.getStartRow());
		if (sl != null && sl.size() > 0) {
			// log.info(sl.size());
			return SUCCESS;
		}
		return INPUT;
	}

	/**
	 * 
	 * Description: 根据学号分页查询所有的学生
	 * 
	 * @Version1.0 Apr 11, 2009 1:14:21 PM 李双江（paradise.lsj@gmail.com）创建
	 * @param studentno
	 * @return
	 */

	public String getAllStudentPerPageByStudentNo(String[] studentno) {
		int totalRow = studentservice.getAllStudentRowsByStudentNo(studentno);
		pager = pagerService.getPager(this.getCurrentPage(), this
				.getPagerMethod(), totalRow);
		this.setCurrentPage(String.valueOf(pager.getCurrentPage()));
		this.setTotalRows(String.valueOf(totalRow));
		sl = this.studentservice.getAllStudentPerPageByStudentNo(studentno,
				pager.getPageSize(), pager.getStartRow());
		if (sl != null && sl.size() > 0) {
			return SUCCESS;
		}
		return INPUT;
	}

	/**
	 * 
	 * Description: 根据学id查询所有的学生
	 * 
	 * @param studentid
	 * @return
	 */

	public JSONArray getAllStudentByStudentid(String studentid) {
		String[] studentids = studentid.split(",");
		sl = this.studentservice.getAllStudentByStudentId(studentids);
		if (sl != null & sl.size() > 0) {
			JSONArray jsonArray = JSONArray.fromObject(sl);
			return jsonArray;
		}
		return null;
	}

	/**
	 * 
	 * Description: 根据学号取得一条学生记录
	 * 
	 * @Version1.0 Mar 24, 2009 10:37:55 PM 李萌（neolimeng@gmail.com）创建 changed by
	 *             郑旭
	 * @return
	 */
	public String searchStudentByStudentNo(String studentNo) {
		try {
			studentNo = studentNo.trim();
			sl = this.studentservice.getStudentByStudentNo(studentNo);
			if (sl != null & sl.size() > 0) {
				// JSON转换date格式
				if (null == sl.get(0).getStudentbirthday()) {
					JSONObject jsonObject = JSONObject.fromObject(sl.get(0));
					return jsonObject.toString();
				} else {
					System.out.println(ReflectionToStringBuilder.toString(
							sl.get(0)).toString());

					String jsonArray = JsonUtil.getJsonString4JavaPOJO(sl
							.get(0),
							DateJsonValueProcessor.DEFAULT_DATE_PATTERN);
					return jsonArray;
				}
			}
			return null;
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 
	 * Description: 由姓名查询学生
	 * 
	 * @Version1.0 Apr 9, 2009 12:35:48 AM 郑旭（zhengxu2006@gmail.com）创建
	 * @param name
	 * @return
	 */
	public JSONArray searchStudentByStudentName(String name, int pageNum,
			int totalCount, HttpSession session) {
		try {
			name = name.trim();
			int totalRow = totalCount; // changed by zhengxu
			DWRPage page = new DWRPage();
			page.setPageProperties(totalRow, 20);
			page.setCurrentPage(pageNum);
			session.setAttribute("page", page);
			int cp = (page.getCurrentPage() - 1) * 20;
			int ps = page.getPageSize();
			sl = this.studentservice.searchStudentByStudentName(name, ps, cp);
			if (sl != null & sl.size() > 0) {
				JSONArray jsonArray = JSONArray.fromObject(sl);
				return jsonArray;
			}
			return null;
		} catch (Exception e) {
			log.error("DMS_error:根据姓名查询学生有误！");
			log.error("DMS_error:错误原因！" + e);
			return null;
		}
	}

	/**
	 * 
	 * Description: 根据姓名模糊查询到的学生的总行数
	 * 
	 * @Version1.0 Apr 23, 2009 2:06:45 AM 郑旭（zhengxu2006@gmail.com）创建
	 * @param studentname
	 * @return
	 */
	public int searchStudentByStudentNameTotalRows(String studentname) {
		return studentservice.searchStudentByStudentNameTotalRows(studentname);
	}

	/**
	 * 
	 * Description: 通过模糊或不全的学号进行查询
	 * 
	 * @Version1.0 Apr 20, 2009 11:52:16 PM 郑旭（zhengxu2006@gmail.com）创建
	 * @param name
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public JSONArray searchStudentByStudentDimNo(String dimno, int pageNum,
			int totalCount, HttpSession session) {
		try {
			dimno = dimno.trim();
			int totalRow = totalCount; // changed by zhengxu
			DWRPage page = new DWRPage();
			page.setPageProperties(totalRow, 20);
			page.setCurrentPage(pageNum);
			session.setAttribute("page", page);
			int cp = (page.getCurrentPage() - 1) * 20;
			int ps = page.getPageSize();
			sl = this.studentservice.searchStudentByStudentDimNo(dimno, ps, cp);
			if (sl != null & sl.size() > 0) {
				JSONArray jsonArray = JSONArray.fromObject(sl);
				return jsonArray;
			}
			return null;
		} catch (Exception e) {
			log.error("DMS_error:根据学号模糊查询学生有误！");
			log.error("DMS_error:错误原因！" + e);
			return null;
		}
	}

	/**
	 * 
	 * Description: 根据学号模糊查询学生得到的总行数
	 * 
	 * @Version1.0 Apr 23, 2009 1:57:06 AM 郑旭（zhengxu2006@gmail.com）创建
	 * @param dimno
	 * @return
	 */
	public int searchStudentByStudentDimNoTotalRows(String dimno) {
		return studentservice.searchStudentByStudentDimNoTotalRows(dimno);

	}

	public String exportStudentsToExcel() {
		try {
			studentInfoForExportToExcel(studentIdListForExcel);
			StudentPoi sp = new StudentPoi();
			HttpServletResponse response = ServletActionContext.getResponse();
			String fname = "detial";// Excel文件名
			OutputStream os = response.getOutputStream();// 取得输出流
			response.reset();// 清空输出流
			response.setHeader("Content-disposition", "attachment; filename="
					+ fname + ".xls");
			// 设定输出文件头,该方法有两个参数，分别表示应答头的名字和值。
			response.setContentType("application/msexcel");
			// 定义输出类型
			// log.info("看看你到底有几行！！"+sl.size());
			sl = this.studentservice.getStudentByStudentNo("0481049");
			// se.writeStudentExcel(sl, os);
			sp.writeStudentExcel(sl, os);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	/**
	 * 
	 * Description: 根据条件高级查询学生
	 * 
	 * @Version1.0 2009-3-27 下午10:50:04 李双江（paradise.lsj@gmail.com）创建
	 * @param searchstudentsql
	 * @return
	 */
	@SuppressWarnings( { "static-access", "unchecked" })
	public JSONArray getStudentBySQLSearchConditionsPerPage(int pageNum,
			int totalCount, String searchstudentsql, HttpSession session) {
		log.info("in action student..");
		List stulist = new ArrayList();
		// searchstudentsql =
		// "select student.*,
		// collegeinfo.collegeinfoname,majorinfo.majorinfoname from
		// student,collegeinfo,majorinfo where 1=1 and student.collegeinfoid=8
		// and student.majorinfoid= majorinfo.majorinfoid and
		// student.collegeinfoid=collegeinfo.collegeinfoid";
		int totalRow = totalCount; // changed by zhengxu
		DWRPage page = new DWRPage();
		page.setPageProperties(totalRow, 20);
		page.setCurrentPage(pageNum);
		session.setAttribute("page", page);
		int cp = (page.getCurrentPage() - 1) * 20;
		int ps = page.getPageSize();
		stulist = studentservice.getStudentBySQLSearchConditions(
				searchstudentsql, ps, cp);
		// Object[] obj = (Object[])stulist.get(0);
		// log.info(obj[7]+"----------------------------------------------");
		if (stulist != null & stulist.size() > 0) {
			JSONArray stulistjson = new JSONArray().fromObject(stulist);
			// log.info("stuList ==" + stulist.get(0).getStudentname());
			return stulistjson;
		}
		return null;
	}

	/**
	 * 
	 * Description: 分页查询指定预分配标记的学生列表
	 * 
	 * @Version1.0 2009-4-5 上午02:03:50 李双江（paradise.lsj@gmail.com）创建
	 * @param pageNum
	 * @param totalCount
	 * @param studentpreallocationid
	 * @param session
	 * @return
	 */
	@SuppressWarnings("static-access")
	public JSONArray getStudentPerPageByPreID(int pageNum, int totalCount,
			String studentpreallocationid, HttpSession session) {
		// studentpreallocationid = "1";
		List<Student> preidStuList = new ArrayList<Student>();
		int totalRow = totalCount;
		DWRPage page = new DWRPage();
		page.setPageProperties(totalRow, 10);
		page.setCurrentPage(pageNum);
		session.setAttribute("stupage", page);
		int cp = (page.getCurrentPage() - 1) * 10;
		int ps = page.getPageSize();
		preidStuList = studentservice.getStudentByStudentPreID(
				studentpreallocationid, ps, cp);
		if (preidStuList != null & preidStuList.size() > 0) {
			// System.out.println("-----------"+ReflectionToStringBuilder.toString(preidStuList.get(1)).toString()+"-----------");
			JSONArray presstujson = new JSONArray().fromObject(preidStuList);
			return presstujson;
		}
		return null;
	}

	/**
	 * 
	 * Description: 查询preassignid的学生的总行数
	 * 
	 * @Version1.0 2009-4-5 上午01:56:36 李双江（paradise.lsj@gmail.com）创建
	 * @param studentpreallocationid
	 * @return
	 */
	public int getStudentByStudentPreIDRows(String studentpreallocationid) {
		return studentservice
				.getStudentByStudentPreIDRows(studentpreallocationid);
	}

	/**
	 * 
	 * Description:返回页面分页对象
	 * 
	 * @Version1.0 2009-3-28 上午11:13:26 李双江（paradise.lsj@gmail.com）创建
	 * @param session
	 * @return
	 */
	@SuppressWarnings("static-access")
	public JSONObject getPage(HttpSession session) {
		JSONObject joa = new JSONObject().fromObject((DWRPage) session
				.getAttribute("page"));
		return joa;
	}

	/**
	 * 
	 * Description: 返回页面分页对象
	 * 
	 * @Version1.0 2009-4-5 上午03:20:11 李双江（paradise.lsj@gmail.com）创建
	 * @param session
	 * @return
	 */
	@SuppressWarnings("static-access")
	public JSONObject getStuPage(HttpSession session) {
		JSONObject joa = new JSONObject().fromObject((DWRPage) session
				.getAttribute("stupage"));
		return joa;
	}

	/**
	 * 
	 * Description: * Description:批量更新出查询学生的预分配id
	 * 
	 * @Version1.0 2009-3-29 下午08:01:34 李双江（paradise.lsj@gmail.com）创建
	 * 
	 * @param allstudentid
	 * @param studentpreassignid
	 * @return
	 */
	public String batchUpdateStudentPreassignedID(String allstudentid,
			String studentpreassignid) {
		String[] studentid = allstudentid.split(",");
		return studentservice.UpdateBatchStudentpreassignedID(studentid,
				studentpreassignid);
	}

	/**
	 * 
	 * Description: 根据传入的sql更新所有高级查询出的学生的studentpreasignedid
	 * 
	 * @Version1.0 Mar 29, 2009 9:10:23 PM 郑旭（zhengxu2006@gmail.com）创建
	 * @param sql
	 * @return
	 */
	public int updateStudentPreasignedid(String sql) {
		int effectLinesNum = this.studentservice
				.updateAllStudentPreasignedidAtService(sql);
		log.info("DMS_info:根据传入的sql更新所有高级查询出的学生,传入的SQL为:" + sql + ",更新的学生数为:"
				+ effectLinesNum);
		return effectLinesNum;
	}

	/**
	 * 
	 * Description: 获得查询的总行数
	 * 
	 * @Version1.0 Mar 30, 2009 12:44:59 PM 郑旭（zhengxu2006@gmail.com）创建
	 * @param sql
	 * @return
	 */
	public int getStudentBySQLSearchConditionsRows(String searchsql) {
		return this.studentservice
				.getStudentBySQLSearchConditionsRows(searchsql);
	}

	/**
	 * 
	 * Description: 根据学号查询学生的基本信息（非全部信息，仅给非超级管理员看的信息）
	 * studentdormitorytype对应dormitory表的dormitoryconcretefuncid
	 * 
	 * @Version1.0 Apr 9, 2009 11:03:51 PM 郑旭（zhengxu2006@gmail.com）创建
	 * @param studentid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String[] getStudentBasicInfo(String studentno,
			int studentdormitorytype) {
		try {
			studentno = studentno.trim();
			List sl = this.studentservice.getStudentBasicInfo(studentno,
					studentdormitorytype);
			if (0 == sl.size()) {
				return null;
			}
			Object[] slist_id = (Object[]) sl.get(0);
			// 这里将返回的数据转为字符串数组传回页面
			if (slist_id != null & slist_id.length > 0) {
				String[] strarr = new String[slist_id.length];
				// 这里是生日,为date类型,要单独处理
				// log.info(slist_id[12].toString().substring(0, 10) + "$$");
				if (null != slist_id[12]) {
					String t = slist_id[12].toString();
					strarr[12] = (t.length() >= 10 ? t.substring(0, 10) : "");
				} else {
					strarr[12] = "";
				}
				// 这里是入住时间,为date类型,要单独处理
				if (null != slist_id[16]) {
					String t = slist_id[16].toString();
					strarr[16] = (t.length() >= 10 ? t.substring(0, 10) : "");
				} else {
					strarr[16] = "";
				}
				// 这里是结束时间,为date类型,要单独处理
				if (null != slist_id[17]) {
					String t = slist_id[17].toString();
					strarr[17] = (t.length() >= 10 ? t.substring(0, 10) : "");
				} else {
					strarr[17] = "";
				}

				for (int a = 0; a < slist_id.length; a++) {
					if (a != 12 && a != 16 && a != 17) {
						if (null == slist_id[a]) {
							strarr[a] = "";
						} else {
							strarr[a] = slist_id[a] + "";
						}
					}
				}
				return strarr;
			}
			return null;
		} catch (Exception e) {
			log.error("DMS_error:根据学号查询学生基本信息有误!");
			log.error("错误原因:" + e);
			return null;
		}
	}

	/**
	 * 
	 * Description: 为导出到Excel而准备的学生数据，不包含住宿信息
	 * 
	 * @Version1.0 Apr 14, 2009 11:14:23 PM 郑旭（zhengxu2006@gmail.com）创建
	 * @param stringOfStuIdArray页面上的被选中的学生的ID数组被转换后的字符串，形式如xxx,xxx,xxx,xxx……
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List studentInfoForExportToExcel(String stringOfStuIdArray) {
		try {
			List list = new ArrayList();
			list = studentservice
					.studentInfoForExportToExcel(stringOfStuIdArray);
			log.info("查到的学生的数量为：" + list.size());
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 
	 * Description: 为导出到Excel而准备的学生数据，包含住宿信息
	 * 
	 * @Version1.0 Apr 14, 2009 11:14:23 PM 郑旭（zhengxu2006@gmail.com）创建
	 * @param stringOfStuIdArray页面上的被选中的学生的ID数组被转换后的字符串，形式如xxx,xxx,xxx,xxx……
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List studentInfoForExportToExcel2(String stringOfStuIdArray) {
		try {
			List list = new ArrayList();
			list = studentservice
					.studentInfoForExportToExcel2(stringOfStuIdArray);
			log.info("查到的学生的数量为：" + list.size());
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 
	 * Description: 高级查询页面：将选中的学生导出到Excel中，不包含住宿信息。
	 * 
	 * @Version1.0 May 22, 2009 11:36:22 AM 李萌（neolimeng@gmail.com）创建
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String exportSelectedStudentsToExcel() throws IOException {
		OutputStream os = null;
		try {
			// TODO 数据库中学生信息不全，需要补齐
			List list = studentInfoForExportToExcel(studentIdListForExcel);
			log.info("@@@" + studentIdListForExcel);

			HttpServletResponse response = ServletActionContext.getResponse();
			String fname = "ExportedSelectedStudentInfo";// Excel文件名
			os = response.getOutputStream();// 取得输出流
			response.reset();// 清空输出流
			response.setHeader("Content-disposition", "attachment; filename="
					+ fname + ".xls");
			// OutputStream os = new FileOutputStream("D:\\abcd.xls");
			// 设定输出文件头,该方法有两个参数，分别表示应答头的名字和值。
			response.setContentType("application/msexcel");
			// 定义输出类型

			String[] titles = { "学号", "姓名", "入学年份", "学制", "身份证", "学院", "专业",
					"班级", "性别", "出生日期", "民族", "政治面貌", "生源地", "毕业学校", "语种",
					"层次", "备注" };
			log.info("=============" + list.size());
			if (null != list && list.size() > 0) {
				Object[] row0 = (Object[]) list.get(0);

				System.out.println("列数：" + row0.length);
				// for(int a = 0;a<row0.length;a++){
				// System.out.println(row0[a]);
				// }
			}
			NeoPoiUtil.poiWriteExcel(titles, list, os);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		} finally {
			os.flush();
			os.close();
		}
	}

	/**
	 * 
	 * Description: 将选中的学生导出到Excel中，包含有住宿信息
	 * 
	 * @Version1.0 Aug 22, 2009 5:30:34 PM 郑旭（zhengxu2006@gmail.com）创建
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String exportSelectedStudentsToExcel2() throws IOException {
		OutputStream os = null;
		try {
			// TODO 数据库中学生信息不全，需要补齐
			List list = studentInfoForExportToExcel2(studentIdListForExcel2);

			HttpServletResponse response = ServletActionContext.getResponse();
			String fname = "ExportedSelectedStudentInfo";// Excel文件名
			os = response.getOutputStream();// 取得输出流
			response.reset();// 清空输出流
			response.setHeader("Content-disposition", "attachment; filename="
					+ fname + ".xls");
			// OutputStream os = new FileOutputStream("D:\\abcd.xls");
			// 设定输出文件头,该方法有两个参数，分别表示应答头的名字和值。
			response.setContentType("application/msexcel");
			// 定义输出类型
			String[] titles = { "学号", "姓名", "性别", "民族", "学院", "专业", "班级", "宿舍",
					"备注" };
			log.info("=============" + list.size());
			NeoPoiUtil.poiWriteExcel(titles, list, os);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		} finally {
			os.flush();
			os.close();
		}
	}

	/**
	 * 
	 * Description: 根据学号查询学生的历史住宿信息
	 * 
	 * @Version1.0 Apr 17, 2009 3:27:26 PM 郑旭（zhengxu2006@gmail.com）创建
	 *             studentdormitorytype对应dormitory表的dormitoryconcretefuncid
	 * @param studentno
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String[][] getStudentHistoryAccomodationInfo(String studentno,
			int studentdormitorytype) {
		try {
			List historyList = this.studentservice
					.getStudentHistoryAccomodationInfo(studentno,
							studentdormitorytype);
			if (historyList != null & historyList.size() > 0) {
				{
					String jsonString = null;
					String[][] strarr = new String[historyList.size()][3];
					String[] strList = new String[3];
					for (int a = 0; a < historyList.size(); a++) {
						Object[] obj = (Object[]) historyList.get(a);
						// jsonString =
						// JsonUtil.getJsonString4JavaPOJO((Date)obj[0],
						// DateJsonValueProcessor.DEFAULT_DATE_PATTERN);
						if (null != obj[0]) {
							String tmp = ((Date) obj[0]).toString();
							jsonString = (tmp.length() >= 10 ? tmp.substring(0,
									10) : "");
						} else {
							jsonString = "";
						}
						strarr[a][0] = jsonString;
						// jsonString =
						// JsonUtil.getJsonString4JavaPOJO((Date)obj[1],
						// DateJsonValueProcessor.DEFAULT_DATE_PATTERN);
						if (null != obj[1]) {
							String t = ((Date) obj[1]).toString();
							jsonString = (t.length() >= 10 ? t.substring(0, 10)
									: "");
						} else {
							jsonString = "";
						}
						strarr[a][1] = jsonString;
						strarr[a][2] = (String) obj[2];
					}

					return strarr;
				}
			}
			return null;
		} catch (Exception e) {
			log.error("DMS_error:根据学号查询学生历史住宿信息有误！");
			log.error("DMS_error:错误原因！" + e);
			return null;
		}
	}

	/**
	 * 
	 * Description: 高级查询页面全选所有查询到的学生
	 * 
	 * @Version1.0 Apr 21, 2009 4:08:42 PM 郑旭（zhengxu2006@gmail.com）创建
	 * @param searchstudentsql
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public JSONArray getStudentBySQL(String searchstudentsql) {
		try {

			List slist_id = new ArrayList();
			slist_id = this.studentservice.getStudentBySQL(searchstudentsql);
			// List historyList =
			// this.getStudentHistoryAccomodationInfo(studentno);
			log.info("根据sql查询到的学生数目" + slist_id.size());
			// log.info("根据ID查询到的学生历史住宿信息数目:" + historyList.size());
			if (slist_id != null & slist_id.size() > 0) {
				{
					JSONArray jsonArray = JSONArray.fromObject(slist_id);
					log.info(slist_id.get(0));
					log.info(jsonArray.get(0) + "$" + jsonArray.get(1));
					return jsonArray;
				}
			}
			return null;
		} catch (Exception e) {
			log.info("DMS_error:高级查询页面全选所有查询到的学生有误！");
			log.info("DMS_error:错误原因！" + e);
			return null;
		}
	}

	/**
	 * 
	 * Description: 高级查询页面全选所有查询到的学生姓名和手机
	 * 
	 * @Version1.0 Apr 21, 2009 4:08:42 PM 郑旭（zhengxu2006@gmail.com）创建
	 * @param searchstudentsql
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public JSONArray getStudentTelBySQL(String searchstudentsql) {
		try {

			List slist_id = new ArrayList();
			slist_id = this.studentservice.getStudentTelBySQL(searchstudentsql);
			log.info("根据sql查询到的学生数目" + slist_id.size());
			if (slist_id != null & slist_id.size() > 0) {
				{
					JSONArray jsonArray = JSONArray.fromObject(slist_id);
					log.info(slist_id.get(0));
					log.info(jsonArray.get(0) + "$" + jsonArray.get(1));
					System.out.println(jsonArray.get(2).toString());
					return jsonArray;
				}
			}
			return null;
		} catch (Exception e) {
			log.info("DMS_error:高级查询页面全选所有查询到的学生姓名和手机有误！");
			log.info("DMS_error:错误原因！" + e);
			return null;
		}
	}

	/**
	 * 
	 * Description: 根据学号获得学生的交费记录
	 * 
	 * @Version1.0 Apr 22, 2009 12:55:32 AM 郑旭（zhengxu2006@gmail.com）创建
	 *             studentdormitorytype对应dormitory表的dormitoryconcretefuncid
	 * @param studentno
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public JSONArray getStudentChargeRecord(String studentno,
			int studentdormitorytype) {
		try {
			studentno = studentno.trim();
			List slist_id = new ArrayList();
			slist_id = this.studentservice.getStudentChargeRecord(studentno,
					studentdormitorytype);
			// List historyList =
			// this.getStudentHistoryAccomodationInfo(studentno);
			// log.info("根据ID查询到的学生历史住宿信息数目:" + historyList.size());
			if (slist_id != null & slist_id.size() > 0) {
				JSONArray jsonArray = JSONArray.fromObject(slist_id);
				return jsonArray;
			}
			return null;
		} catch (Exception e) {
			log.info("DMS_error:根据学号获得学生的交费记录有误！");
			log.info("DMS_error:错误原因！" + e);
			return null;
		}
	}

	/**
	 * 
	 * Description: 根据某一条件查询某一个或某一批学生是否存在 ,返回人数(增加学生时验证输入的学号是否存在时用到)
	 * 
	 * @Version1.0 Apr 27, 2009 10:10:29 PM 郑旭（zhengxu2006@gmail.com）创建
	 * @param arg
	 * @param sno
	 * @return
	 */
	public int isExistStu(String arg, String sno) {
		try {
			return this.studentservice.isExistStu(arg, sno);
		} catch (Exception e) {
			log.info("DMS_error:根据某一条件查询某一个或某一批学生是否存在有误！");
			log.info("DMS_error:错误原因！" + e);
			return 0;
		}
	}

	/**
	 * 
	 * Description: 返回满足特定studentpreallocationid的studentid列表
	 * 
	 * @Version1.0 May 20, 2009 9:37:06 PM 李双江（paradise.lsj@gmail.com）创建
	 * @param studentpreallocationid
	 * @return
	 */
	public String[][] getStudentIDListByStudentPreID(
			String studentpreallocationid) {
		return studentservice
				.getStudentIDListByStudentPreID(studentpreallocationid);
	}

	/**
	 * 
	 * Description: 编辑学生的备注，在学生基本信息页面用到
	 * 
	 * @Version1.0 Jul 18, 2009 11:05:06 PM 郑旭（zhengxu2006@gmail.com）创建
	 * @param msc
	 * @param stuno
	 * @return
	 */
	public boolean editStuMsc(String msc, String stuno) {
		return studentservice.editStuMsc(msc, stuno);
	}

	/**
	 * 从大厅跳转到单人分配页面后用到
	 * 
	 * @param id
	 * @return
	 */
	public String getPersonById(int id) {
		Student s = this.studentservice.getStudent(id);
		return s.getStudentno();
	}

	/**
	 * 获取搜索的学生JSON数据
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getSearchedStudentJSON() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		int type;
		String term;
		int start;
		int limit;
		int totalProperty;
		JSONArray jsonArray;
		List<Student> studentList;
		String jsonString;
		try {
			type = Integer.parseInt(request.getParameter("type"));
			term = request.getParameter("term");
			if (term != null && term.trim().length() > 0) {
				try {
					start = Integer.parseInt(request.getParameter("start"));
				} catch (NumberFormatException e1) {
					start = 0;
				}
				try {
					limit = Integer.parseInt(request.getParameter("limit"));
				} catch (NumberFormatException e1) {
					limit = 5;
				}
				if (type == 0) {// 0是按照学号模糊查询，1为按照姓名
					totalProperty = studentservice
							.searchStudentByStudentDimNoTotalRows(term);
					studentList = studentservice.searchStudentByStudentDimNo(
							term, limit, start);
				} else {
					totalProperty = studentservice
							.searchStudentByStudentNameTotalRows(term);
					studentList = studentservice.searchStudentByStudentName(
							term, limit, start);
				}
				if (studentList != null & studentList.size() > 0) {
					jsonArray = JSONArray.fromObject(studentList);
					response.setContentType("text/html;charset=UTF-8");// 解决中文乱码
					jsonString = "{start:" + start + ",limit:" + limit
							+ ",totalProperty:" + totalProperty + ",infoList:"
							+ jsonArray.toString() + "}";
				} else {
					jsonString = "{start:0,limit:" + limit
							+ ",totalProperty:0,infoList:[]}";
				}
				try {
					response.getWriter().write(jsonString);
				} catch (IOException e) {
					log.error(e);
				}
			}
		} catch (Exception e) {
		}

		return null;
	}
}
